﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineRailway.Administrator;
using System.Data.SqlClient;
using System.Data;
using OnlineRailway.EF;
using OnlineRailway.DAL;


namespace OnlineRailway.UI.Administrator
{
    public partial class AccountAdd : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMes.Text = "";

        }

        [WebMethod]
        public static string CheckExistUser(string name)
        {
            String rs=null;
            try
            {
                using (var context = new RailwayOnlineEntities())
                {
                    var obj= context.Accounts.First(x => x.UserName == name);
                    rs = obj!=null ? "Exist" : "OK";
                }
            }
            catch (Exception)
            {
                rs = "OK";
            }
            
            return rs;
        }

        private string Encryption(String pass)
        {
            String rs = pass;
            for (var i = 0; i < 4; i++)
            {
                if (i != 0)
                {
                    rs += System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(rs.Trim(), "SHA1");
                    rs += System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(rs.Trim(), "MD5");
                }
                else
                {
                    rs = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(rs.Trim(), "SHA1");
                }
            }
            return rs;
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            var imgPath = "~/StorageImage/AccountImages/" + fuImage.FileName;
            if (fuImage.PostedFile != null && fuImage.PostedFile.FileName != "")
            {
                if (fuImage.PostedFile.ContentLength > 1048576)
                {
                    lblMes.Text = "• File is too big! File must be less than 1MB.";
                    return;
                }
                else
                {
                    fuImage.SaveAs(Server.MapPath(imgPath));
                }
            }
            else
            {
                imgPath = "";
            }
            var un = Request.Form["txtUsername"];
            int roles=0;
            switch (slRole.Value)
            {
                case "Admin":
                    roles = 0;
                    break;
                case "Staff":
                    roles = 1;
                    break;
                default:
                    roles = 2;
                    break;
            }
            var obj = new Account
            {
                UserName = Request.Form["txtUsername"],
                FullName = Request.Form["txtFullname"],
                Password = Encryption(Request.Form["txtPassword"]),
                Age = Convert.ToDateTime(BirthMonth.Value + "/" + Request.Form["txtDay"] + "/" + Request.Form["txtYear"]),
                Gender = slGender.Value,
                Address = Request.Form["txtAddress"],
                Telephone = Request.Form["txtTelephone"],
                Email = Request.Form["txtEmail"],
                Images = imgPath,
                Qualification = Request.Form["txtQualification"],
                Role = roles
            };
            var objDAL = new Account_DAL();
            objDAL.AddAccount(obj);
            lblMes.Text = "Account " + un + " has been create successfully!";
            
        }

        

        
    }
}